﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VideoClassLibrary.Model;
using VideoClassLibrary.BusinessLogicLayer;
using System.IO;
public partial class SchoolManagement_AddSchool : AdminUserBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.SchoolID != 0)
        {
            btnAdd.Visible = false;
            btnReset.Visible = false;

            SchoolModule sm = new SchoolModule();
            V_School school = sm.GetSchoolModelById(this.CurrentUser.SchoolID);
            this.rbtnSchoolType.SelectedValue = school.VS_type.ToString();
            this.txtVS_name.Text = school.VS_name;
            txtVS_name.ReadOnly = true;
            this.txtVS_englishname.Text = school.VS_englishname;
            txtVS_englishname.ReadOnly = true;
            this.txtVS_phone.Text = school.VS_phone;
            txtVS_phone.ReadOnly = true;
            this.txtVS_address.Text = school.VS_address;
            txtVS_address.ReadOnly = true;
            this.txtVS_zipcode.Text = school.VS_zipcode;
            txtVS_zipcode.ReadOnly = true;
            this.txtVS_city.Text = school.VS_city;
            txtVS_city.ReadOnly = true;
            this.txtVS_province.Text = school.VS_province;
            txtVS_province.ReadOnly = true;
            ddlSchoolState.SelectedValue = school.VS_state.ToString();
            this.txtVS_relanname.Text = school.VS_relanname;
            txtVS_relanname.ReadOnly = true;
            this.txtVS_relanamephone.Text = school.VS_relanamephone;
            txtVS_relanamephone.ReadOnly = true;
            this.txtVS_relanameemail.Text = school.VS_relanameemail;
            txtVS_relanameemail.ReadOnly = true;
            txtWeb.Text = school.Vs_web;
        }

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {

        string fileExtension = string.Empty;
        //if (fileExtension.ToLower() != "jpg" && fileExtension.ToLower() == "gif")
        //{
        //    PageHelper.AlterMsg("图片格式只支持jpg/gif");
        //    return;
        //}
        
       
        V_School school = new V_School();
        SchoolModule sm = new SchoolModule();
        school.create_time = DateTime.Now;
        school.update_time = DateTime.Now;
        school.VS_type = Convert.ToInt32(rbtnSchoolType.SelectedValue.Trim());
        school.VS_name = this.txtVS_name.Text;
        school.VS_englishname = this.txtVS_englishname.Text;
        school.VS_phone = this.txtVS_phone.Text;
        school.VS_address = this.txtVS_address.Text;
        school.VS_zipcode = this.txtVS_zipcode.Text;
        school.VS_city = this.txtVS_city.Text;
        school.VS_province = this.txtVS_province.Text;
        school.VS_state = Convert.ToInt32(this.ddlSchoolState.SelectedValue.Trim());
        //school.VS_logofile = "/Images/logo" + fileExtension;
        school.VS_relanname = this.txtVS_relanname.Text;
        school.VS_relanamephone = this.txtVS_relanamephone.Text;
        school.VS_relanameemail = this.txtVS_relanameemail.Text;
        school.Vs_desc = this.txtDesc.Text;
        school.Vs_web = txtWeb.Text;
        //school.Vs_SchoolImg = "/Images/SchoolImg" + fileExtension;
        if (!string.IsNullOrEmpty(this.schfile.FileName))//修改图片
        {
            fileExtension = System.IO.Path.GetExtension(this.schfile.FileName);
            if (fileExtension != "" && fileExtension.ToLower() != ".jpg" && fileExtension.ToLower() != ".gif")
            {
                PageHelper.AlterMsg("图片格式只支持jpg/gif");
                return;
            }
        }

        if (!string.IsNullOrEmpty(this.fuSchoolImage.FileName))//修改图片
        {
            fileExtension = System.IO.Path.GetExtension(fuSchoolImage.FileName);
            if (fileExtension != "" && fileExtension.ToLower() != ".jpg" && fileExtension.ToLower() != ".gif")
            {
                PageHelper.AlterMsg("图片格式只支持jpg/gif");
                return;
            }
        }

        int schoolId = sm.AddNewSchool(school);
        if (schoolId > 0)
        {
            if (!string.IsNullOrEmpty(this.schfile.FileName))//修改图片
            {
                fileExtension = System.IO.Path.GetExtension(this.schfile.FileName);
                if (fileExtension != "" && fileExtension.ToLower() != ".jpg" && fileExtension.ToLower() != ".gif")
                {
                    PageHelper.AlterMsg("图片格式只支持jpg/gif");
                    return;
                }
                string dictionarySavedPath = PageHelper.GetWebConfigValue("SchoolImagePath");
                dictionarySavedPath += "\\Images\\SchoolId\\";
                dictionarySavedPath = dictionarySavedPath.Replace("SchoolId", schoolId.ToString());
                if (!Directory.Exists(dictionarySavedPath))
                {
                    Directory.CreateDirectory(dictionarySavedPath);
                }
                // dictionarySavedPath = Server.MapPath(PageHelper.ApplicationPath) + PageHelper.GetWebConfigValue("VideoPath").Replace("SchoolId", currentSchoolId.ToString());   
                //保存logo的文件夹绝对路径

                string logfileSavedPath = dictionarySavedPath + "logo" + fileExtension;
                //fileSavedPath = dictionarySavedPath + "/logo" + fileExtension;
                this.schfile.SaveAs(logfileSavedPath);

                school.VS_logofile = ("/Images/SchoolId/logo" + fileExtension).Replace("SchoolId", schoolId.ToString());
                sm.SaveLogoFile(schoolId.ToString(), school.VS_logofile);
            }
            if (!string.IsNullOrEmpty(this.fuSchoolImage.FileName))//修改图片
            {
                fileExtension = System.IO.Path.GetExtension(fuSchoolImage.FileName);
                if (fileExtension != "" && fileExtension.ToLower() != ".jpg" && fileExtension.ToLower() != ".gif")
                {
                    PageHelper.AlterMsg("图片格式只支持jpg/gif");
                    return;
                }
                string dictionarySavedPath = PageHelper.GetWebConfigValue("SchoolImagePath");
                dictionarySavedPath += "\\Images\\SchoolId\\";
                dictionarySavedPath = dictionarySavedPath.Replace("SchoolId", schoolId.ToString());
                if (!Directory.Exists(dictionarySavedPath))
                {
                    Directory.CreateDirectory(dictionarySavedPath);
                }
                // dictionarySavedPath = Server.MapPath(PageHelper.ApplicationPath) + PageHelper.GetWebConfigValue("VideoPath").Replace("SchoolId", currentSchoolId.ToString());   
                //保存logo的文件夹绝对路径

                string logfileSavedPath = dictionarySavedPath + "SchoolImg" + fileExtension;
                //fileSavedPath = dictionarySavedPath + "/logo" + fileExtension;
                this.fuSchoolImage.SaveAs(logfileSavedPath);
                school.Vs_SchoolImg = ("/Images/SchoolId/SchoolImg" + fileExtension).Replace("SchoolId", schoolId.ToString());
                sm.SaveSchoolImageFile(schoolId.ToString(), school.Vs_SchoolImg);
            }
            PageHelper.AlterMsgAndRedirect("添加学校成功！", "SearchAndSchool.aspx");          
        }
        else
        {
            lblMess.Text = "添加学校失败,请稍后再试！";

           // PageHelper.AlterMsg("添加学校失败,请稍后再试！");
        }

    }
   
}
